<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/9/12
 * Time: 2:09 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_ReportedProblems extends Page {
	/**
	 * @var Db_ReportedProblems
	 */
	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_ReportedProblems';
		$this->actionsTableObject = array(
			'ajaxLoadDataAction',
			'editReportProblemAction',
			'saveAction'
		);
		parent::__construct();
	}

	public function defaultAction() {

		$projects = Db_Projects::getDistinctProjects();
		self::$TPL->assign('projects', $projects);

		$types = Db_ReportedProblems::getEnumValues('rp_type');
		self::$TPL->assign('types', $types);

		$statuses = Db_ReportedProblems::getEnumValues('rp_status');
		self::$TPL->assign('statuses', $statuses);

		self::$TPL->appendJsFile('admin/reported_problems');
		self::$TPL->setBodyTemplate('list_reported_problems');
		return array(
			'type' => 'display'
		);
	}

	public function ajaxLoadDataAction() {
		$allColumns = array('nr', 'pr_name', 'rp_email', 'rp_phone', 'rp_problem_content', 'rp_country', 'rp_type', 'rp_status');
		$searchColumns = array('pr_name', 'rp_email', 'rp_phone', 'rp_problem_content', 'rp_country', 'rp_type', 'rp_status');

		list($searchQuery, $orderBy, $limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, "Db_ReportedProblems", "getTotalReportedProblems");

		$reportedProblems = $this->tableObject->getAllReportedProblems($limit, $searchQuery, $orderBy);
		self::$TPL->assign('reportedProblems', $reportedProblems);

		return array(
			'type' => 'encoded_json',
			'content' => self::$TPL->display('json_reported_problems', true)
		);
	}

	public function editReportProblemAction() {
		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'details'), '?action=editReportProblem', true);

			$reportedProblem = Db_ReportedProblems::getReportedProblemById($this->tableObject->objectArray->id);
			self::$TPL->assign('reportedProblem', $reportedProblem);

			$types = Db_ReportedProblems::getEnumValues('rp_type');
			self::$TPL->assign('types', $types);

			$statuses = Db_ReportedProblems::getEnumValues('rp_status');
			self::$TPL->assign('statuses', $statuses);

			self::$TPL->setBodyTemplate('edit_reported_problem');
			return array(
				'type' => 'display'
			);
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);

	}

	public function saveAction() {
		perm('REPORTED_PROBLEMS_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		$this->tableObject->rp_type = post('rp_type', 'str');
		$this->tableObject->rp_status = post('rp_status', 'str');
		if(!empty($this->tableObject->rp_type) && !empty($this->tableObject->rp_status)) {
			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function ajaxSaveAction() {
		$reportedTypeId = str_replace('rp_type_', '', post('id', 'str'));
		$reportedType = post('value', 'str');
		if(!empty($reportedTypeId) && !empty($reportedType)) {
			$reportedProblems = new Db_ReportedProblems($reportedTypeId);
			$reportedProblems->rp_type = $reportedType;
			if(!$reportedProblems->save()) {
				$reportedType = $reportedProblems->objectArray->rp_type;
			}
		}
		return array(
			'type' => 'encoded_json',
			'content' => t(T_REPORTED_PROBLEMS_TYPE, $reportedType)
		);
	}

	public function ajaxSaveStatusAction() {
		$reportedStatusId = str_replace('rp_status_', '', post('id', 'str'));
		$reportedStatus = post('value', 'str');
		if (!empty($reportedStatusId)){
			$reportedProblems = new Db_ReportedProblems($reportedStatusId);
			$reportedProblems->rp_status = $reportedStatus;
			if(!$reportedProblems->save()) {
				$reportedStatus = $reportedProblems->objectArray->rp_status;
			}
		}
		return array(
				'type' => 'encoded_json',
				'content' => t(T_REPORTED_PROBLEMS_STATUS, $reportedStatus)
		);
	}


	public function ajaxGetTypeAction() {
		$reportedProblemTypes = Db_ReportedProblems::getEnumValues('rp_type');
		foreach($reportedProblemTypes as $reportedProblemType) {
			$reportedProblemTypes['types'][$reportedProblemType] = t(T_REPORTED_PROBLEMS_TYPE, $reportedProblemType);
		}
		return array(
			'type' => 'json',
			'content' => $reportedProblemTypes['types']
		);
	}

	public function ajaxGetStatusAction() {
		$reportedProblemStatuses = Db_ReportedProblems::getEnumValues('rp_status');
		foreach($reportedProblemStatuses as $reportedProblemStatus) {
			$reportedProblemStatuses['status'][$reportedProblemStatus] = t(T_REPORTED_PROBLEMS_STATUS, $reportedProblemStatus);
		}

		return array(
			'type' => 'json',
			'content' => $reportedProblemStatuses['status']
		);
	}

	public function ajaxGetUsersAction() {
		$users = Db_Accounts_Users::getUsersByGroupName('developers');
		return array(
			'type' => 'json',
			'content' => $users
		);
	}

	public function ajaxSaveUserAction() {
		$reportedId = str_replace('rp_user_', '', post('id', 'str'));
		$reportedUserId = post('value', 'str');
		$reportedStatus = null;
		if (!empty($reportedId)){
			$userName = new Db_Accounts_Users($reportedUserId);
			$reportedStatus = $userName->objectArray->u_login;
			$reportedProblems = new Db_ReportedProblems($reportedId);
			$reportedProblems->rp_user_id = $reportedUserId;
			if(!$reportedProblems->save()) {
				$userName = new Db_Accounts_Users($reportedProblems->objectArray->rp_user_id);
				$reportedStatus = $userName->objectArray->u_login;
			}
		}
		return array(
			'type' => 'encoded_json',
			'content' => $reportedStatus
		);
	}

}
